package com.gccloud.tgyfirst.controller.Bro;

import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.controller.SuperController;
import com.gccloud.starter.core.dto.RepeatDTO;
import com.gccloud.starter.mybatis.page.PageVO;
import com.gccloud.starter.plugins.validator.ValidatorUtils;
import com.gccloud.tgyfirst.bro.dto.BroDto;
import com.gccloud.tgyfirst.bro.entity.BroEntity;
import com.gccloud.tgyfirst.bro.vo.BroVo;
import com.gccloud.tgyfirst.service.bro.service.IBroService;
import com.gccloud.tgyfirst.service.bro.service.Impl.BroServiceImpl;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import com.gccloud.starter.plugins.validator.group.Insert;
import com.gccloud.starter.plugins.validator.group.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bro")
@Api(tags = "我的兄弟")
@ApiSort(value = 11)

public class BroController extends SuperController {

    @Autowired
    private BroServiceImpl broService;

    /**
     *
     * @param searchDTO  searchDTO
     * @return success
     */
    @GetMapping("/")
    @ApiOperation(value = "列表", position = 10, notes = "分页查询列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<BroVo>> getPage(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) SearchDTO searchDTO) {
        PageVO<BroEntity> page = broService.getPage(searchDTO);
        PageVO PageVO = BeanConvertUtils.convert(page, PageVO.class);
        PageVO.setList(page.getList());
        return success(PageVO);
    }

    @PostMapping(value = "/getBroList")
    @ApiOperation(value = "查询兄弟列表", position = 10, notes = "分页查询兄弟列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "sex", value = "性别", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "hometown", value = "家乡", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "hobby", value = "爱好", paramType = "query", dataType = "string"),

    })
    public List<BroEntity> getBroListListQx(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) BroEntity broEntity) {
        return broService.queryAllQx(broEntity);
    }
    /**
     * 详情页面
     * @param id id
     * @return 1
     */
    @ApiOperation(value = "详情", position = 20, notes = "兄弟详情", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/{id}")
    public R<BroVo> getById(@ApiParam(name = "待办事件ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        BroEntity broEntity = broService.getByIdWithDp(id);
        BroVo broVo = BeanConvertUtils.convert(broEntity, BroVo.class);
        return success(broVo);
    }


    @PostMapping
    //@RequiresPermissions("bro:add")
    @ApiOperation(value = "新增", position = 30, notes = "新增兄弟", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> add(@ApiParam(name = "新增待办事件JSON对象", value = "传入json格式", required = true) @RequestBody BroDto broDto) {
        ValidatorUtils.validateEntity(broDto, Insert.class);
        BroEntity broEntity = BeanConvertUtils.convert(broDto, BroEntity.class);
        broService.add(broEntity);
        return success(broEntity.getId());
    }


    @PutMapping
    //@RequiresPermissions("bro:update")
    @ApiOperation(value = "修改", position = 40, notes = "修改兄弟", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> upd(@ApiParam(name = "修改待办事件JSON对象", value = "传入json格式", required = true) @RequestBody BroDto broDto) {
        ValidatorUtils.validateEntity(broDto, Update.class);
        BroEntity broEntity = BeanConvertUtils.convert(broDto, BroEntity.class);
        broService.upd(broEntity);
        return success();
    }


    @DeleteMapping("/{id}")
    //@RequiresPermissions("bro:delete")
    @ApiOperation(value = "删除", notes = "删除兄弟", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> del(@ApiParam(name = "待办事件ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        broService.del(id);
        return success();
    }


    @PostMapping("/repeat")
    @ApiOperation(value = "查重", notes = "查重", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Boolean> repeat(@ApiParam(name = "查重", value = "传入JSON对象", required = true) @RequestBody RepeatDTO repeatDTO) {
        ValidatorUtils.validateEntity(repeatDTO);
        boolean repeat = broService.repeat(repeatDTO.getId(), repeatDTO.getName(), repeatDTO.getValue(), GlobalConst.RepeatStrategy.CREATE_BY);
        return success(repeat);
    }

    @PostMapping(value = "/getBroList.do")
    @ApiOperation(value = "详情", position = 11, notes = "查询兄弟", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> getBroList(@ApiParam(name = "查询兄弟", value = "传入JSON对象", required = true)BroEntity broEntity) {
        return broService.queryAll(broEntity);
    }


}


